package cn.zn.modules.pz.cur.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.zn.framework.common.exception.RRException;
import cn.zn.framework.common.utils.FileUtils;
import cn.zn.framework.common.vo.FinanceValues;
import cn.zn.modules.pz.cur.dao.TpAttachmentMapper;
import cn.zn.modules.pz.cur.entity.TpAttachment;
import cn.zn.modules.pz.cur.service.TpAttachmentService;
import cn.zn.modules.pz.enums.ProcessAttachTypeEnum;
import cn.zn.modules.pz.his.entity.vo.PzAttachmentVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TpAttachmentServiceImpl extends ServiceImpl<TpAttachmentMapper, TpAttachment> implements TpAttachmentService {

    @Autowired
    FileUtils fileUtils;
    @Autowired
    FinanceValues financeValues;

    @Override
    public List<TpAttachment> findEntityByMainId(String mainId) {
        QueryWrapper<TpAttachment> queryWrapper = Wrappers.query();
        queryWrapper.eq("TP_MAIN_ID_", mainId);
        queryWrapper.orderByDesc("CREATE_TIME_");
        return this.list(queryWrapper);
    }

    @Override
    public List<PzAttachmentVO> findByMainId(String mainId) {
        return this.findEntityByMainId(mainId).stream().map(o -> {
            PzAttachmentVO pzAttachmentVO = new PzAttachmentVO(financeValues.getFile().getDomain());
            BeanUtils.copyProperties(o, pzAttachmentVO);
            return pzAttachmentVO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<PzAttachmentVO> saveAttach(ProcessAttachTypeEnum processAttachTypeEnum, String mainId, MultipartFile[] fileupload) {
        try {
            LocalDate now = LocalDate.now();
            Collection<TpAttachment> tpAttachments = CollUtil.newArrayList();
            String fileName = "";
            TpAttachment tpAttachment;
            File outputFile;
            for (MultipartFile multipartFile : fileupload) {
                fileName = multipartFile.getOriginalFilename();
                outputFile = fileUtils.updateFile(multipartFile.getInputStream(), fileName.substring(fileName.lastIndexOf(".") + 1), now.toString().split("-"));
                tpAttachment = new TpAttachment();
                tpAttachment.setTpMainId(mainId);
                tpAttachment.setAttachmentType(processAttachTypeEnum.toString());
                tpAttachment.setContentType(multipartFile.getContentType());
                tpAttachment.setGenerateName(outputFile.getName().substring(0, outputFile.getName().lastIndexOf(".")));
                tpAttachment.setPathLocal(outputFile.getPath());
                tpAttachment.setPathRelative(fileUtils.getNoBasePath(outputFile.getPath()).replace(File.separator, "/"));
                tpAttachment.setRealName(fileName.substring(0, fileName.lastIndexOf(".")));
                tpAttachment.setSize(multipartFile.getSize());
                tpAttachment.setSuffix(fileName.substring(fileName.lastIndexOf(".") + 1));
                tpAttachment.setIsMain(false);
                tpAttachments.add(tpAttachment);
            }
            this.saveBatch(tpAttachments);
            return tpAttachments.stream().map(o -> {
                PzAttachmentVO pzAttachmentVO = new PzAttachmentVO(financeValues.getFile().getDomain());
                BeanUtils.copyProperties(o, pzAttachmentVO);
                return pzAttachmentVO;
            }).collect(Collectors.toList());
        } catch (IOException e) {
            log.error("", e);
            throw new RRException("");
        }
    }

    @Override
    public void deleteAttach(List<String> ids) {
        List<TpAttachment> tpAttachments = baseMapper.selectBatchIds(ids);
        tpAttachments.forEach(o -> {
            FileUtil.del(o.getPathLocal());
            baseMapper.deleteById(o.getId());
        });
    }
}
